<!--
 * @Author: Wushiqi
 * @Descripttion: 
 * @Date: 2021-04-26 18:02:59
 * @LastEditor: Wushiqi
 * @LastEditTime: 2021-05-01 21:13:57
-->
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    // 原型链继承
    // const P = function() {
    //   this.name = ['wsq', 'csd']
    // }
    // let Child = function() {}
    // Child.prototype = new P() // 将父级的实例作为子级的原型
    // const c = new Child()
    // c.name.push('njk')
    // console.log(c.name); // ["wsq", "csd", "njk"]

    // const d = new Child()
    // console.log(d.name); //["wsq", "csd", "njk"]子类的实例共享了父类构造函数的引用属性

    // 构造函数继承
    // const A = function(content) {
    //   this.name = ['a', 'b']
    //   this.content = content
    // }
    // const B = function(content) {
    //   A.call(this, content)
    // }
    // const a = new A('Hello')
    // a.name.push('c')
    // console.log(a.name); // ["a", "b", "c"]
    // console.log(a.content); // Hello

    // const b = new B()
    // console.log(b.name); // ["a", "b"]
    // console.log(b.content); // undefined

    // 组合继承（原型链继承+构造函数继承）
    const A = function(name) {
      this.arr = ['a', 'b']
      this.name = name
    }
    A.prototype.sayName = function() {
      console.log(this.name);
    }
    let B = function(name) {
      A.call(this, name) // 构造函数继承特点
    }
    B.prototype = new A() // 原型链继承特点
    const a = new A('Hello')
    a.sayName() // Hello
    a.arr.push('c', 'd')
    console.log(a.arr); // ["a", "b", "c", "d"]

    const b = new B('wsq')
    b.sayName() // wsq
    b.arr.push('fff')
    console.log(b.arr);
  </script>
</body>
</html>